package com.jinlia.show.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinlia.show.modules.base.model.po.ErrandOrderPO;
import com.jinlia.show.modules.base.model.query.DeliveryOrderQuery;
import com.jinlia.show.modules.base.model.query.ErrandOrderQuery;
import com.jinlia.show.modules.base.model.vo.DeliveryOrderVO;
import com.jinlia.show.modules.base.model.vo.ErrandOrderVO;
import com.jinlia.show.modules.base.system.entity.ErrandOrder;
import com.jinlia.show.modules.base.system.service.ErrandOrderMapperService;
import com.jinlia.show.modules.base.system.service.OrderLogMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/errand/dada")
public class ErrandDadaController {
    @Autowired()
    private ErrandOrderMapperService errandOrderMapperService;
    @Autowired
    private OrderLogMapperService orderLogMapperService;

    @GetMapping("{id}")
    public ErrandOrderPO getById(@PathVariable(name = "id") Long id) {
        return errandOrderMapperService.getPOById(id);
    }

    /**
     * 查询订单费用
     *
     * @param orderId
     * @return
     */
    @GetMapping("query/fee/{orderId}")
    public ErrandOrder queryDeliverFee(@PathVariable(name = "orderId") Long orderId) {
        return errandOrderMapperService.queryDeliverFee(orderId);
    }

    /**
     * 查询后新增
     *
     * @param errandOrderId
     * @return
     */
    @PostMapping("query/then/{errandOrderId}")
    public JSONObject addAfterQuery(@PathVariable(name = "errandOrderId") Long errandOrderId) {
        JSONObject jsonObject = errandOrderMapperService.addAfterQuery(errandOrderId);
        orderLogMapperService.addOrderLog("PT", errandOrderId, "呼叫跑腿", "");
        return jsonObject;
    }

    /**
     * 新增订单
     *
     * @return
     */
    @GetMapping("add/order/{deliveryOrderId}")
    public JSONObject addOrder(@PathVariable(name = "deliveryOrderId") Long deliveryOrderId) {
        JSONObject jsonObject = errandOrderMapperService.addOrder(deliveryOrderId);
        return jsonObject;

    }

    /**
     * 查询订单
     *
     * @return
     */
    @GetMapping("order/query/{deliveryOrderId}")
    public JSONObject orderQuery(@PathVariable("deliveryOrderId") Long deliveryOrderId) {
        return errandOrderMapperService.orderQuery(deliveryOrderId);

    }

    /**
     * 查询棋手位置=>返回经纬度信息
     *
     * @return
     */
    @GetMapping("transporter/position/{deliveryOrderId}")
    public JSONObject transporterPosition(@PathVariable("deliveryOrderId") Long deliveryOrderId) {
        return errandOrderMapperService.transporterPosition(deliveryOrderId);

    }

    /**
     * 查询棋手位置=>返回H5信息
     *
     * @return
     */
    @GetMapping("transporter/track/{deliveryOrderId}")
    public JSONObject transporterTrack(@PathVariable("deliveryOrderId") Long deliveryOrderId) {
        return errandOrderMapperService.transporterTrack(deliveryOrderId);

    }

    /**
     * 商户取消订单
     *
     * @return
     */
    @PostMapping("cancel")
    public JSONObject orderCancel(@RequestBody ErrandOrder errandOrder) {
        JSONObject jsonObject = errandOrderMapperService.cancelOrder(errandOrder);
        orderLogMapperService.addOrderLog("PT", errandOrder.getId(), "取消跑腿", "", String.format("取消原因:%S", errandOrder.getCancelReason()));
        return jsonObject;
    }

    /**
     * 重新下单
     *
     * @return
     */
    @PostMapping("/re/add/order/{errandOrderId}")
    public JSONObject reAddOrder(@PathVariable("errandOrderId") Long errandOrderId) {
        orderLogMapperService.addOrderLog("PT", errandOrderId, "重新下单", "");
        return errandOrderMapperService.reAddOrder(errandOrderId);
    }

    @PostMapping("/page")
    public ResponseEntity<Page<ErrandOrderVO>> getPage(@RequestBody ErrandOrderQuery queryParams) {
        Page<ErrandOrderVO> pagePO = errandOrderMapperService.getPagePO(queryParams);
        return ResponseEntity.ok(pagePO);
    }


    /**
     * 查询订单费用
     *
     * @param errandOrderQuery
     * @return
     */
    @PostMapping("list")
    public List<ErrandOrderPO> getList(@RequestBody ErrandOrderQuery errandOrderQuery) {
        return errandOrderMapperService.getList(errandOrderQuery);
    }
}
